<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        inline
        label-width="120px"
        label-position="right"
      >
        <template>
          <el-col :span="24">
            <el-form-item>
              <div class="el-page-header__content" slot="label">
                问卷消息模板
              </div>
              <el-tooltip
                style="padding: 0px 5px 0px 5px"
                class="item"
                effect="light"
                content="设置消息模板时，确保标题含有{title},内容含有{content}"
                placement="top-start"
              >
                <i
                  class="icon-ym icon-ym-extend-exclamation-triangle"
                  style="color: #909399"
                ></i>
              </el-tooltip>
              <el-select
                v-model="workOrderMessage"
                placeholder="请选择"
                style="width: 80%"
              >
                <el-option
                  v-for="item in messageList"
                  :key="item.id"
                  :label="item.fullName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import { isEmpty } from "@/utils/isEmpty";
import { getList } from "@/api/system/messageTemplate";
import { saveByKeyName, getConfigByKeyName } from "@/api/system/sysConfig";
export default {
  components: {},
  props: [],
  data() {
    return {
      title: "消息模板设置",
      formMessage: {
        workOrderMessage: null,
      },
      workOrderMessage: "",
      messageList: [],
      visible: false,
      loading: false,
      listQuery: {
        currentPage: 1,
        pageSize: 20000,
        sort: "desc",
        sidx: "",
      },
      dataForm: {
        code: "",
        name: "",
        parentId: "",
        description: "",
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    init() {
      this.visible = true;

      // 消息模板列表
      this.getMessageList();
    },
    getMessageList() {
      getList(this.listQuery).then((res) => {
        this.messageList = res.data.list;
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.formMessage.keyValue = this.workOrderMessage;
          this.formMessage.keyName = "questionnaireMessage";
          this.formMessage.name = "问卷调查消息模板";
          this.formMessage.category = "questionnaire";
          saveByKeyName(this.formMessage)
            .then((res) => {
              this.$message.success(res.msg);
              this.visible = false;
            })
            .catch(() => {
              this.visible = false;
            });
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/questionnaire/OqQuestionnaireType",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/questionnaire/OqQuestionnaireType/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      this.dataForm = _dataAll;
    },
  },
};
</script>
